﻿using System;
using System.IO;
using System.Windows.Forms;

namespace LxWinFormTools
{
    public partial class LxMainForm : Form
    {
        public LxMainForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string filepath = string.Empty;
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "请选择文件夹路径...";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { 
                filepath = folderBrowserDialog.SelectedPath;
            }
            textBox1 .Text = filepath;


            /*OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;//设置是否可以多选
            ofd.Title = "请选择要处理的文件";//窗口title
            ofd.Filter = "所有文件（*.*）|*.*";//筛选文本文件
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                filepath = ofd.FileName;
            }
            textBox1.Text = filepath;*/

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.execOperate(true);
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void LxMainForm_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void radioBtn_chooseType1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioBtn_chooseType2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void textBox_OutputLog_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            this.execOperate(false);
        }

        /**
         * 执行操作
         */
        private void execOperate(Boolean realExec) {
            Console.WriteLine("===>本次执行是" + (realExec ? "【真实操作】" : "【测试模式】"));

            if (realExec)
            {
                DialogResult dialogResult = MessageBox.Show("本次操作不可逆，请检查确认是否需要执行", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.None);//弹出MessageBox窗口

                if (dialogResult == DialogResult.Cancel)

                {
                    MessageBox.Show("确定取消本次操作", "警告", MessageBoxButtons.OK, MessageBoxIcon.None);
                    return;
                }

            }

            string checkOperateType;
            if (radioBtn_chooseType1.Checked)
            {
                checkOperateType = "1";
            }
            else if (radioBtn_chooseType2.Checked)
            {
                checkOperateType = "2";
            }
            else
            {
                MessageBox.Show("请选择操作类型", "警告", MessageBoxButtons.OK, MessageBoxIcon.None);//弹出MessageBox窗口
                return;
            }


            string chooseFilePath = textBox1.Text;
            textBox_OutputLog.Text = "当前选择的文件夹：【" + chooseFilePath + "】";
            textBox_OutputLog.AppendText("\r\n");
            string[] subFilesList = Directory.GetFiles(chooseFilePath);
            Boolean execResult = false;
            foreach (string singleFilePath in subFilesList)
            {
                // c#根据绝对路径获取 带后缀文件名、后缀名、文件名。
                string oldFileName = Path.GetFileName(singleFilePath);
                // 文件所在目录的绝对路径
                string parentFilePath = Path.GetDirectoryName(singleFilePath);
                // 新文件名
                string newFileName = "";
                if (checkOperateType == "1")
                {
                    if (string.IsNullOrEmpty(textBox_needRepStr.Text))
                    {
                        MessageBox.Show("请输入需要操作的字符串", "警告", MessageBoxButtons.OK, MessageBoxIcon.None);//弹出MessageBox窗口
                        return;
                    }
                    newFileName = oldFileName.Replace(textBox_needRepStr.Text, textBox_newStr.Text);
                    if (realExec)
                    {
                        File.Move(singleFilePath, Path.Combine(Path.GetDirectoryName(singleFilePath), newFileName));
                        execResult = true;
                    }
                }
                else if (checkOperateType == "2")
                {
                    int startIdx = oldFileName.IndexOf("(");
                    int endIdx = oldFileName.IndexOf(")");
                    int length = endIdx + 1 - startIdx;
                    if (startIdx > -1 && endIdx > -1)
                    {
                        string willBeDeleted = oldFileName.Substring(startIdx, length);
                        newFileName = oldFileName.Replace(willBeDeleted, "");
                    }
                    else
                    {
                        newFileName = oldFileName;
                    }
                    if (realExec) {
                        try
                        {
                            File.Move(singleFilePath, Path.Combine(Path.GetDirectoryName(singleFilePath), newFileName));
                            execResult = true;
                        }
                        catch (IOException ex)
                        {
                            MessageBox.Show("【" + singleFilePath + "】" + "重命名为：【" + newFileName + "】操作失败，失败原因：" + ex.Message,
                                "异常", MessageBoxButtons.OK, MessageBoxIcon.None);//弹出MessageBox窗口
                        }
                    }
                }
                else
                {
                    newFileName = oldFileName;
                }

                textBox_OutputLog.AppendText(singleFilePath + "=====>新路径【" + Path.Combine(Path.GetDirectoryName(singleFilePath), newFileName) + "】");
                textBox_OutputLog.AppendText("\r\n");
            }
            if (execResult)
            {
                MessageBox.Show("操作成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.None);//弹出MessageBox窗口
            }
        }

    }
}
